﻿using ConsoleApplication.WebAPI;
using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Permission;
using Kingdee.BOS.Expression;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
//using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;

namespace HM
{
    [HotUpdate]
    [Description("物流跟踪状态=已签收，直接调拨单自动下推销售出库单")]
    public class AutoPushDown : IScheduleService
    {
        /// <summary>

        /// 执行计划入口函数

        /// </summary>

        /// <param name="ctx"></param>

        /// <param name="schedule"></param>

        public void Run(Context ctx, Schedule schedule)
        {
            try
            {
                string text = "/*dialect*/" + string.Format("select distinct FSalBillNo from V_SAL_LOGISTICSINFO t\r\n                left join T_SAL_LOGISTINFODETAIL t1 on t.fid=t1.FID\r\n                where FTraceStatus='3' and FOBJECTTYPEID='STK_TransferDirect' and FSalBillNo not in (\r\n                select t.FBILLNO from T_STK_STKTRANSFERIN t\r\n\t\t\t\tleft join T_STK_STKTRANSFERINENTRY t1 on t.FID=t1.FID\r\n\t\t\t\tleft join T_SAL_OUTSTOCKENTRY_LK t2 on t2.FSBILLID=t1.FID and t2.FSID=t1.FENTRYID\r\n\t\t\t\twhere t2.FSBILLID is not null)", Array.Empty<object>());
                DynamicObjectCollection dynamicObjectCollection = DBUtils.ExecuteDynamicObject(ctx, text, null, null, CommandType.Text, Array.Empty<SqlParam>());
                bool flag = dynamicObjectCollection.Count > 0;
                if (flag)
                {
                    for (int i = 0; i < dynamicObjectCollection.Count; i++)
                    {
                        string str = Convert.ToString(dynamicObjectCollection[i]["FSalBillNo"]);
                        string text2 = InvokeHelper.Login();
                        int num = Extensions.Value<int>(JObject.Parse(text2)["LoginResultType"]);
                        bool flag2 = num == 1;
                        if (flag2)
                        {
                            string text3 = "{\"Ids\":\"\",\"Numbers\":['" + str + "'],\"EntryIds\":\"\",\"RuleId\":\"TransferDirect-OUTSTOCK\",\"TargetBillTypeId\":\"\",\"TargetOrgId\":0,\"TargetFormId\":\"SAL_OUTSTOCK\",\"IsEnableDefaultRule\":\"false\",\"IsDraftWhenSaveFail\":\"false\",\"CustomParams\":{}}";
                            text2 = InvokeHelper.Push("STK_TransferDirect", text3.ToString());
                            JObject jobject = JObject.Parse(text2);
                            string a = Convert.ToString(jobject["Result"]["ResponseStatus"]["IsSuccess"]);
                            bool flag3 = a == "True";
                            if (flag3)
                            {
                                JToken jtoken = jobject["Result"]["ResponseStatus"]["SuccessEntitys"][0]["Id"];
                                string str2 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"";
                                JToken jtoken2 = jtoken;
                                string text4 = str2 + ((jtoken2 != null) ? jtoken2.ToString() : null) + "\",\"SelectedPostId\":0,\"NetworkCtrl\":\"\",\"IgnoreInterationFlag\":\"\"}";
                                text2 = InvokeHelper.Submit("SAL_OUTSTOCK", text4.ToString());
                                string str3 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"";
                                JToken jtoken3 = jtoken;
                                string text5 = str3 + ((jtoken3 != null) ? jtoken3.ToString() : null) + "\",\"InterationFlags\":\"\",\"NetworkCtrl\":\"\",\"IsVerifyProcInst\":\"\",\"IgnoreInterationFlag\":\"\"}";
                                text2 = InvokeHelper.Audit("SAL_OUTSTOCK", text5.ToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
    }
}
